package com.atung.parking.controller;

import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.entity.BusMessagePublish;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.MessageState;
import com.atung.parking.enums.MessageType;
import com.atung.parking.enums.ParkingIotApprovalState;
import com.atung.parking.mapstruct.MessagePublishConverter;
import com.atung.parking.request.ByIdRequest;
import com.atung.parking.request.query.QueryMessageRequest;
import com.atung.parking.request.save.SaveOrUpdateMessageRequest;
import com.atung.parking.response.page.MessagePublishResponse;
import com.atung.parking.service.MessagePublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RestController
@Api(tags = "信息发布")
@RequestMapping("/api/message")
public class MessagePublishController {
    @Resource
    private MessagePublishService messagePublishService;
    @Resource
    private MessagePublishConverter messagePublishConverter;

    @PostMapping("/app/save")
    @ApiOperation("(小程序)用户发布消息")
    @PermissionValid(roleType = {AdminType.USER, AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult savaMessage(@RequestBody @Valid SaveOrUpdateMessageRequest request) {
        return messagePublishService.saveMessageService(request);
    }

    @GetMapping("/app/queryOneself")
    @ApiOperation("（小程序）用户获取自己消息")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult<List<MessagePublishResponse>> queryOneselfMessageList(@RequestParam MessageType messageType) {

        List<MessagePublishResponse> messagePublishResponses=messagePublishService.queryOneselfMessageService
                (messageType);
        return ApiResult.ok(messagePublishResponses);
    }

    @GetMapping("/query")
    @ApiOperation("(管理/小程序)获取全部消息")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.USER})
    public ApiResult<PageResponse<MessagePublishResponse>> superQueryList(QueryMessageRequest request) {
        if (Objects.isNull(request.getMessageType())) {
            return ApiResult.fail("请选择消息类型");
        }
        PageInfoResponse<BusMessagePublish> publishPageResponse = messagePublishService.superQueryListService(request);
        return ApiResult.ok(publishPageResponse.getData().stream().map(messagePublishConverter::toResponse)
                .collect(Collectors.toList()), request.getPageNum(), request.getPageSize(), publishPageResponse.getTotal());
    }

    @GetMapping("/export")
    @ApiOperation("导出消息")
//    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public void exportMessageList(QueryMessageRequest request, HttpServletResponse httpServletResponse) {
        if (Objects.isNull(request.getMessageType())) {
            throw new RuntimeException("请求参数为空");
        }
        messagePublishService.exportMessageListService(request, httpServletResponse);
    }

    @PostMapping("/delete")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    @ApiOperation("删除消息")
    public ApiResult updateMessageState(@RequestBody @Valid
                                                ByIdRequest request ) {
        return messagePublishService.updateMessageStateService(request.getId());
    }
    @GetMapping("/state")
    @ApiOperation("获取消息状态下拉数据")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult<List<Map<String, String>>> getMessageStateList() {
        List<Map<String, String>> messageStateList = new ArrayList<>();
        for (MessageState messageState : MessageState.values()) {
            Map<String, String> messageStateMap = new HashMap<>();
            messageStateMap.put("name", messageState.getDescription());
            messageStateMap.put("value", messageState.name());
            messageStateList.add(messageStateMap);
        }
        return ApiResult.ok(messageStateList);
    }
}
